﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing.Fields;
using AgileShare.Framework.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgileShare.Framework.Entities
{
	public abstract class SPEntityBase
	{
		public SPListItem ListItem { get; set; }

		public SPEntityBase(SPListItem listItem)
		{
			this.ListItem = listItem;
			this.IsValidEntity = null;
		}

		public SPEntityBase(SPListItem listItem, SPContentTypeId contentTypeId)
		{
			if (listItem.ContentTypeId == contentTypeId || listItem.ContentTypeId.IsChildOf(contentTypeId))
			{
				this.ListItem = listItem;
				this.IsValidEntity = true;
			}
			else
			{
				this.IsValidEntity = false;
				TraceProvider.Log(TraceProviderCategories.Default, (uint)TraceProviderEventIds.Entities, TraceProviderSeverity.Monitorable,
					"Invalid cast to Entity. List Item Content Type Name: {0}; Content Type Id: {1}. SPEntity Content Type Id: {2};", listItem.ContentType.Name, listItem.ContentTypeId, contentTypeId);
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is valid entity.
		/// </summary>
		/// <value>
		/// <c>true</c> if this instance is valid entity; otherwise, <c>false</c>.
		/// </value>
		public bool? IsValidEntity { get; set; }

		protected object GetFieldValue(Guid fieldId)
		{
			return ListItem[fieldId];
		}

		protected void SetFieldValue(Guid fieldId, object value)
		{
			ListItem[fieldId] = value;
		}

		protected string GetTextFieldValue(Guid fieldId)
		{
			return Convert.ToString(ListItem[fieldId]);
		}

		protected void SetTextFieldValue(Guid fieldId, string value)
		{
			ListItem[fieldId] = value;
		}

		protected double? GetNumberFieldValue(Guid fieldId)
		{
			return (ListItem[fieldId] != null) ? (double?)ListItem[fieldId] : null;
		}

		protected void SetNumberFieldValue(Guid fieldId, double? value)
		{
			ListItem[fieldId] = value;
		}

		protected int? GetIntegerFieldValue(Guid fieldId)
		{
			return (ListItem[fieldId] != null) ? (int?)ListItem[fieldId] : null;
		}

		protected void SetIntegerFieldValue(Guid fieldId, int? value)
		{
			ListItem[fieldId] = value;
		}

		protected SPFieldMultiChoiceValue GetMultiChoiceFieldValue(Guid fieldId)
		{
			return ListItem[fieldId] != null ? new SPFieldMultiChoiceValue(ListItem[fieldId].ToString()) : new SPFieldMultiChoiceValue();
		}

		protected void SetMultiChoiceFieldValue(Guid fieldId, SPFieldMultiChoiceValue value)
		{
			ListItem[fieldId] = value;
		}

		protected DateTime? GetDateTimeFieldValue(Guid fieldId)
		{
			return (ListItem[fieldId] != null) ? (DateTime?)ListItem[fieldId] : null;
		}

		protected void SetDateTimeFieldValue(Guid fieldId, DateTime? value)
		{
			ListItem[fieldId] = value;
		}

		protected bool? GetBooleanFieldValue(Guid fieldId)
		{
			return (ListItem[fieldId] != null) ? (bool?)ListItem[fieldId] : null;
		}

		protected void SetBooleanFieldValue(Guid fieldId, bool? value)
		{
			ListItem[fieldId] = value;
		}

		protected SPFieldUrlValue GetUrlFieldValue(Guid fieldId)
		{
			return ListItem[fieldId] != null ? new SPFieldUrlValue(ListItem[fieldId].ToString()) : new SPFieldUrlValue();
		}

		protected void SetUrlFieldValue(Guid fieldId, SPFieldUrlValue value)
		{
			ListItem[fieldId] = value;
		}

		protected SPFieldUserValue GetUserFieldValue(Guid fieldId)
		{
			return ListItem[fieldId] != null ? new SPFieldUserValue(ListItem.ParentList.ParentWeb, ListItem[fieldId].ToString()) : new SPFieldUserValue();
		}

		protected void SetUserFieldValue(Guid fieldId, SPFieldUserValue value)
		{
			ListItem[fieldId] = value;
		}

		protected SPFieldLookupValue GetLookupFieldValue(Guid fieldId)
		{
			return ListItem[fieldId] != null ? new SPFieldLookupValue(ListItem[fieldId].ToString()) : new SPFieldLookupValue();
		}

		protected void SetLookupFieldValue(Guid fieldId, SPFieldLookupValue value)
		{
			ListItem[fieldId] = value;
		}

		protected SPFieldLookupValueCollection GetLookupMultiFieldValue(Guid fieldId)
		{
			return ListItem[fieldId] != null ? new SPFieldLookupValueCollection(ListItem[fieldId].ToString()) : new SPFieldLookupValueCollection();
		}

		protected void SetLookupMultiFieldValue(Guid fieldId, SPFieldLookupValueCollection value)
		{
			ListItem[fieldId] = value;
		}

		protected ImageFieldValue GetImageFieldValue(Guid fieldId)
		{
			return ListItem[fieldId] != null ? new ImageFieldValue(ListItem[fieldId].ToString()) : new ImageFieldValue();
		}

		protected void SetImageFieldValue(Guid fieldId, ImageFieldValue value)
		{
			ListItem[fieldId] = value;
		}

		protected LinkFieldValue GetLinkFieldValue(Guid fieldId)
		{
			return ListItem[fieldId] != null ? new LinkFieldValue(ListItem[fieldId].ToString()) : new LinkFieldValue();
		}

		protected void SetLinkFieldValue(Guid fieldId, LinkFieldValue value)
		{
			ListItem[fieldId] = value;
		}

		protected MediaFieldValue GetMediaFieldValue(Guid fieldId)
		{
			return (MediaFieldValue)ListItem[fieldId];
		}

		protected void SetMediaFieldValue(Guid fieldId, MediaFieldValue value)
		{
			ListItem[fieldId] = value;
		}



	}
}
